package api_demo;

import api_demo.PushResultDto;
import api_demo.ZkHttpDto;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.PhoneUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import dto.ApplyBo;
import lombok.extern.slf4j.Slf4j;
import utils.AESEncryptUtils;
import utils.MD5Util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 众银分期api对接
 */
@Slf4j
public class demo {

    private static final Integer HTTP_RESPONSE_CODE_SUCCESS = 200;

    //撞库
    public static ZkHttpDto zk(String phone, String cityId) {
        ZkHttpDto instance = ZkHttpDto.getInstance();
        // 封装请求数据
        Map<String, Object> requestDataMap = MapUtil.newHashMap();
        requestDataMap.put("phoneMd5", MD5Util.encode(phone));
        String paramJson = JSONUtil.toJsonStr(requestDataMap);
        instance.setParam(paramJson);
        // 调用第三方接口
        String response = HttpRequest.post("https://www.zhongheyinhua.com.cn/api/channel/clue/md5/_check")
                .header("channel", "test")//请求头
                .body(paramJson)//表单内容
                .timeout(3000)//超时，毫秒
                .execute().body();
        instance.setResponse(response);
        JSONObject responseObj = JSONUtil.parseObj(response);
        JSONObject data = responseObj.getJSONObject("data");
        Integer status = data.getInt("status");
        instance.setIsZk(status);
        return instance;
    }


    //推送线索
    public static PushResultDto pushClue(ApplyBo applyBo, String url, boolean isEncrypt) {
        PushResultDto pushResultDto = PushResultDto.getInstance();
        String response = null;
        Integer integerOne = new Integer(1);
        // 封装请求数据
        Map<String, Object> requestDataMap = MapUtil.newHashMap();
        requestDataMap.put("channel", "JKT");

        // 城市编码
        requestDataMap.put("city", applyBo.getCityId());
        // 线索池编号
        requestDataMap.put("poolNo", "10001");
        // 用户姓名
        requestDataMap.put("name", applyBo.getName());
        // 手机号
        if(PhoneUtil.isMobile(applyBo.getPhone())){ //判断是否为加密手机号
            requestDataMap.put("phone",applyBo.getPhone());
        }else {
//            String phone = DESUtils.decrypt(applyBo.getPhone());
//            requestDataMap.put("phone",phone);
        }
        // 性别 0男，1女，2未知
        requestDataMap.put("sex", integerOne.equals(applyBo.getSex())?"0":"1");
        // 房产 1按揭房，2全款房，3无
        requestDataMap.put("house", integerOne.equals(applyBo.getHouseProperty())?"1":"3");
        // 车 1按揭车，2全款车，3无
        requestDataMap.put("car", integerOne.equals(applyBo.getCarProperty())?"1":"3");
        // 社保
        requestDataMap.put("socialSecurity", integerOne.equals(applyBo.getHas())?"1":"2");
        // 公积金
        requestDataMap.put("accumulationFund", integerOne.equals(applyBo.getFund())?"1":"2");
        // 人寿保单
        requestDataMap.put("lifeInsurance", integerOne.equals(applyBo.getBdStatus())?"1":"2");
        // 年龄
        requestDataMap.put("age", String.valueOf(applyBo.getAge()));
        List<Map<String, Object>> list = new ArrayList<>();
        list.add(requestDataMap);
        String paramJson = JSONUtil.toJsonStr(list);
        log.info("===paramJson:{}===",paramJson);

        if (isEncrypt) {// 加密请求
            String secretKey = "abcdefg";
            String requestJson = AESEncryptUtils.encrypt(paramJson, secretKey);
            //String encryptStr = AESEncryptUtils.encodeByECB(secretKey.getBytes(), requestJson.getBytes()).toString();
            String nonce = AESEncryptUtils.getRandomStr(16);
            String timestamp = Long.toString(AESEncryptUtils.getTimeStamp());
            String sign = AESEncryptUtils.getSignature(requestJson, nonce, secretKey, timestamp);
            // 调用第三方接口
            response = HttpRequest.post(url)
                    .contentType("text/plain")
                    .header("channel", "JKT")
                    .header("nonce", nonce)
                    .header("timestamp", timestamp)
                    .header("sign", sign)
                    .body(requestJson)//表单内容
                    .timeout(3000)//超时，毫秒
                    .execute().body();
        } else {// 非加密请求
            // 调用第三方接口
            response = HttpRequest.post(url)
                    .header("channel", "test")//请求头
                    .body(paramJson)//表单内容
                    .timeout(3000)//超时，毫秒
                    .execute().body();
        }
        log.info("===res:{}===",response);
        pushResultDto.setPushResult(response);
        JSONObject responseObj = JSONUtil.parseObj(response);
        pushResultDto = PushResultDto.getInstance();
        pushResultDto.setPushStatus(HTTP_RESPONSE_CODE_SUCCESS.equals(responseObj.getInt("code")));
        return pushResultDto;
    }

    //测试方法
    public static void main(String[] args) {
        //撞库测试
        ZkHttpDto zk = zk("13788889999", "510100");
        System.out.println(zk);

        //推送测试
        ApplyBo applyBo = new ApplyBo();
        applyBo.setName("测试");
        applyBo.setPhone("13788889999");
        applyBo.setZhimaCredit(1);
        applyBo.setHas(1);
        applyBo.setFund(1);
        applyBo.setHouseProperty(1);
        applyBo.setCarProperty(1);
        applyBo.setBdStatus(1);
        applyBo.setSex(1);
        applyBo.setAge(18);
        applyBo.setCity("成都市");
        applyBo.setCityId("510100");
        PushResultDto pushResultDto = pushClue(applyBo, "https://www.zhongheyinhua.com.cn/api/channel/clue",true);
    }


}
